$(document).ready(function(){
    // create dialog for message form
    var sendMessageDialog = new DialogBox($('#sendMessage'))
    
    $('#sendNewMessage').click(function(){
        MAX.ajax('GET', $(this).attr('href'), '', function(res){
            sendMessageDialog.html(res.html)
            sendMessageDialog.open()
        })
        return false
    })
    $('#sendMessage').on('submit','#sendMessageForm',function(e){
        MAX.ajax('POST',$(this).attr('action'),$(this).serialize(),function(res){
            if(res.valid)
            {
                sendMessageDialog.close()
                $('input[name=selectMessage]').first().attr('checked',true)
                $('input[name=selectMessage]').trigger('change')
            }
            else
            {
                sendMessageDialog.html(res.html)
            }
        })
        return false
    })
})
